<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Db;
class Project extends Common
{
public function index(){
    $projects=Db::name("projects")->select();
  	$this->assign("projects",$projects);
  	return $this->fetch();
  }

public function jiekou(){
  $rs=db('projects')->select();
  return json($rs);
}

 public function add(){
    if (request()->isPost()) {
     //创建数组，给video表的所有字段赋值
      $data=[
       'p_name'=>input('param.p_name'),
        'p_notic'=>input('param.p_notic'),
        'p_address'=>input('param.p_address'),
        'p_price'=>input('param.p_price'),
        'p_tel'=>input('param.p_tel'),
        'p_intro'=>input('param.p_intro'),
        'p_time'=>input('param.p_time'),
      ];

     $validate = \think\Loader::validate('Project');
if(!$validate->check($data)){
    $this->error($validate->getError());
}
      //存入数据库
    $res=Db::name("projects")->insert($data);
    //处理结果
    if ($res) {
      $this->success("添加项目成功");
    }else{
      $this->error("添加项目失败");
    }
    }
  }

 public function edit(){
    $p_id=input('p_id');
    $projects=Db::name("projects")->find($p_id);
    echo json_encode($projects);
  }

 public function update(){
      $data=[
        'p_name'=>input('param.p_name'),
        'p_notic'=>input('param.p_notic'),
        'p_address'=>input('param.p_address'),
        'p_price'=>input('param.p_price'),
        'p_tel'=>input('param.p_tel'),
        'p_intro'=>input('param.p_intro'),
        'p_time'=>input('param.p_time'),
      ];
$p_id=input('param.p_id');
var_dump($data);
//修改数表
      if(Db::name('projects')->where("p_id",$p_id)->update($data)){
        $this->success('修改成功');
      }else{
        $this->error('修改失败');
      }
    }


public function delete($id){
    //先删除头像：find方法和unlink
    if (Db::name("projects")->delete($id)) 
      {  echo Db::name("projects")->getLastSql();exit;
      $this->success('删除成功');
    }else $this->error('删除失败');
  }

/*public function page(){
$page = Db::name('projects')->where('status',1)->paginate(2);
// 把分页数据赋值给模板变量list
$this->assign('page', $page);
// 渲染模板输出
return $this->fetch();
}*/

}